import { crtChannelKs } from "./crtChannelKs";
import crtkc from "./crtkc";
import { User } from "../scripts/UserManager";
import { UserData } from "../scripts/UserConst";
import { Event } from "../scripts/EventManager";
import { default as GameEvent } from "../scripts/EventConst";
/**全民战争*/
export class crtChannelKs4 extends crtChannelKs {
    channelObj: any = (window as any).ks;
    paraMap: Map<String, string> = new Map([
        ['appId', 'ks676665844106029313'],
        ['vedioAd', '2300020054_01'],
        ['insertAd', '2300020054_02'],
        ['bannerAd', '2300020054_03'],
        ['ctrlId', '67bd673cc8946b1700d95cc3'], //策略开头id
        ['gid', ''], //gid
    ]);
    /** 游戏名称，用于远程存档,隐私政策等显示用 */
    gameName: string = '全民战争';

    ruanzhu: string = '著作权人：南京游义道网络科技有限公司\n登记号：2020SR0763097';
    init(): void {
        super.init()
    }
    /** 分渠道的广告特定动作，actId区分哪个动作,para为执行动作需要的对象
     * 1:恭喜过关页面   0:关闭视频广告,隐藏文字  1:勾选状态  2:隐藏文字,但是视频广告正常显示
     * 2:体力特权/体力补给站/钥匙补给站加文字 观看视频免费领取   0:不勾选  1:勾选
     * 3:钥匙补给,体力补给,关卡提示,恭喜过关弹插屏    0:关闭   1:开启
     * 4:每三关过关界面去除看视频获取奖励文字，2个按钮都正常展示视频广告。0为关闭视频广告，1为开启
     * 5: 跳过添加视频按钮
     * 6: 展示九宫格宝箱
     * 7:登录奖励
     */
    doAction(actId: number, para: any) {
        let watchAd = () => {
            this.showVideo(() => {

            });
        }
        if (para && para[0] == 'ks4') {
            console.log("ks4策略", actId)
            //window.__prv.doAction(1, ['ks4']);
            if (actId == 1) {
                let isOpen = this.getCtrl('p2');//所有的二级弹窗
                if (isOpen) {
                    this.showInter();

                }
            }
            else if (actId == 2) {
                let ctrlState = this.getCtrl('p4');
                switch (ctrlState) {
                    case 0:
                        para[1].active = false;
                        para[2].active = para[1].active

                        break;
                    case 1:
                        para[1].active = true;
                        para[2].active = para[1].active
                        break
                    case 2:
                        para[1].parent.active = false
                        para[2].active = true
                        break
                }
            }
            else if (actId == 3) {

                para[1].active = true;

            }
            else if (actId == 4) {
                //window.__prv.doAction(4, ['ks4']);
                let ctrlState = this.getCtrl('p5');
                ctrlState = 1
                switch (ctrlState) {
                    case 0:
                        para[1].active = false;
                        para[2].active = false;
                        para[3].active = para[2].active
                        break;
                    case 1:
                        para[1].active = true;
                        para[2].active = true;
                        para[3].active = para[2].active
                        break;
                    case 2:
                        para[1].active = false;
                        para[2].active = true;
                        para[3].active = para[2].active
                        break;
                    default:
                        break;
                }
            } else if (actId == 5) {
                this.winCount++
                let isOpen = this.getCtrl('p7');//不管输赢，每玩三局去掉“观看视频获取体力”，两个按钮的视频标识正常显示。0为不显示勾选及字，1为显示勾选及字
                if (isOpen && this.winCount % 3 == 0) {
                    para[1].active = true
                    para[2].active = para[1].active
                    para[1].parent.active = true

                } else {
                    para[1].active = false
                    para[2].active = true
                    para[1].parent.active = false
                }
            } else if (actId == 6) {
                //每成功过两关弹出“关卡奖励”，0为关闭，1为开启
                //1.可以直接点1-3个不等的宝箱，其他宝箱要看广告领取
                //2.“领取奖励”前面有和按钮一样颜色的视频观看标识，中途关闭视频没有奖励，看完视频给10个体力值
                //3.宝箱里面有1个体力补给
                this.showRewardFrameCount++
                let isOpen = this.getCtrl('p8');
                if (isOpen && this.showRewardFrameCount % 2 == 0) {
                    //@ts-ignore
                    window.__prv.showRewardBox()

                }
            }
            else if (actId == 7) {
                console.log("cc.sys.localStorage.getItem('firstime')", cc.sys.localStorage.getItem("firstime"))
                if (cc.sys.localStorage.getItem("firstime") == null) {
                    cc.sys.localStorage.setItem("walkFast_power", 20)
                    cc.sys.localStorage.setItem("firstime", true)
                } else {

                }
            }

        }
    }
    winCount = 0
    showRewardFrameCount = 0
    doAction1(actId: number, para: any) {
        let watchAd = () => {
            this.showVideo(() => {

            });
        }
        if (para && para[0] == 'ks4') {
            if (actId == 1) {
                let ctrlState = this.getCtrl('p2');
                switch (ctrlState) {
                    case 0:
                        para[1].active = false;

                        break;
                    case 1:
                        para[1].active = true;
                        para[1].opacity = 255
                        break
                    case 2:
                        para[1].active = true;
                        para[1].opacity = 0
                        break;
                    default:
                        break;
                }
            } else if (actId == 2) {
                para[1].active = true;
                let isOpen = this.getCtrl('p3');
                if (isOpen) {
                    para[2].active = true;
                    para[3].active = false;
                } else {
                    para[2].active = false;
                    para[3].active = true;
                }
            } else if (actId == 3) {
                let isOpen = this.getCtrl('p4');
                if (isOpen) {
                    this.showInter();
                }
            } else if (actId == 4) {
                let isOpen = this.getCtrl('p6');
                if (isOpen) {
                    para[1].active = false;
                } else {
                    para[1].active = true;
                }
            } else if (actId == 5) {
                para[2].active = true;
                para[3].active = true;
            } else if (actId == 6) {
                //@ts-ignore
                window.__prv.showRewardFrame();
            } else if (actId == 7) {
                //@ts-ignore
                window.__prv.loginReward();
            }
            else if (actId == 8) {
                para[1].on("click", () => {
                    let isOpen = this.getCtrl('p9') > Math.random() * 100;
                    if (isOpen) {
                        this.showVideo(() => {
                            let key = User.get(UserData.KEY);
                            User.set(UserData.KEY, key + 1);
                            // Tip.show("钥匙+" + 1);
                            Event.emit(GameEvent.KEY_EFFECT, true);
                            para[2].active = false
                        })

                    }

                })
                //@ts-ignore
                //window.__prv.loginReward();
            }
            else if (actId == 9) {
                console.error("doAction 9")
                para[1].active = true
                para[2].on("click", () => {
                    let isOpen = this.getCtrl('p10') > Math.random() * 100;
                    if (isOpen) {
                        this.showVideo(() => {
                            let key = User.get(UserData.KEY);
                            User.set(UserData.KEY, key + 1);
                            // Tip.show("钥匙+" + 1);
                            Event.emit(GameEvent.KEY_EFFECT, true);
                        })

                    }
                    para[1].active = !para[1].active
                    if (!para[1].active) {
                        para[3].active = false
                    } else {
                        para[3].active = true
                        para[3].opacity = 255
                    }
                })
                if (!para[1].active) {
                    para[3].active = false
                } else {
                    para[3].active = true
                    para[3].opacity = 255
                }

            } else if (actId == 10) { //AD隐藏
                let isOpen = this.getCtrl('p3');
                if (isOpen) { //AD显示
                    para[1].active = true
                    para[2].active = false
                } else {
                    para[1].active = false
                    para[2].active = true
                }


            } else if (actId == 11) { //延时弹出
                let isOpen = this.getCtrl('p8');
                if (isOpen) {
                    para[1].active = false
                    setTimeout(() => {
                        para[1].active = true
                    }, 3000);
                } else {
                    para[1].active = true
                }
            }

        }
    }



    showVideo(succ): void {
        console.error('观看视频');
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            succ && succ();
            return;
        }
        if (!this.getCtrl('p1')) {
            succ && succ();
            return;
        }
        const id = this.getPara('vedioAd');
        console.error('ks showVideo1');
        if (!id) {
            return;
        }

        cc.game.pause();
        this.rewardedVideoAd = this.channelObj.createRewardedVideoAd({
            adUnitId: id
        });
        if (this.videoFns['onLoad']) {
            this.rewardedVideoAd.offLoad(this.videoFns['onLoad']);
        }
        if (this.videoFns['onError']) {
            this.rewardedVideoAd.offError(this.videoFns['onError']);
        }
        if (this.videoFns['onClose']) {
            this.rewardedVideoAd.offClose(this.videoFns['onClose']);
        }
        this.rewardedVideoAd.offLoad();
        this.rewardedVideoAd.offError();
        this.rewardedVideoAd.offClose();
        const me = this;
        me.videoFns['onLoad'] = () => {
            crtkc.log('激励视频 广告加载成功');
        };
        me.videoFns['onError'] = (err) => {
            crtkc.err(err);
            cc.game.resume();
            // 视频失败切换分享
            this.channelObj.showToast({
                title: '暂无广告',
            });
        }
        me.videoFns['onClose'] = (res) => {
            // 用户点击了【关闭广告】按钮
            // 小于 2.1.0 的基础库版本，res 是一个 undefined
            cc.game.resume();
            if (res && res.isEnded) {
                // 正常播放结束，可以下发游戏奖励
                succ && succ();
            } else {
            }
        };
        me.rewardedVideoAd.onLoad(me.videoFns['onLoad']);
        me.rewardedVideoAd.onError(me.videoFns['onError']);
        me.rewardedVideoAd.onClose(me.videoFns['onClose']);

        me.rewardedVideoAd.show()
            .then(() => {

            }).catch(err => {
                me.rewardedVideoAd.load().then(() => {
                    // 加载成功后需要再显示广告
                    return me.rewardedVideoAd.show().then(() => {

                    }).catch((err) => {
                        cc.game.resume();
                    });
                }).catch((err) => {
                    cc.game.resume();
                });
            });

    }

    showInter(isBanner: boolean = false, style?: any) {
        const id = this.getPara('insertAd');
        if (!id) {
            return;
        }

        //原生插屏延迟
        const p5 = this.getCtrl('p3');
        const me = this;
        setTimeout(() => {
            me.doShowInter(isBanner, style);
        }, p5 * 1000);
    }


}